package com.lh.glorypavilion.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.glorypavilion.result.R;
import com.lh.glorypavilion.service.edu.entity.Teacher;
import com.lh.glorypavilion.service.edu.entity.vo.TeacherVo;
import com.lh.glorypavilion.service.edu.service.ITeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lhui
 * @since 2020-10-12
 */
//@CrossOrigin
@Api(description = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Resource
    private ITeacherService teacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("list")
    public R teacherList(){
        List<Teacher> teachers = teacherService.list();
        return R.ok().data("data",teachers);
    }

    @ApiOperation(value = "id删除讲师和阿里云oss头像",notes = "逻辑删除")
    @DeleteMapping("remvoceOne/{id}")
    public R removeOne(@ApiParam("讲师ID") @PathVariable("id") String id){

        //删除头像
        teacherService.removeAvatarById(id);
        //删除讲师
        boolean b = teacherService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("数据不存在");
    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable("page") Long page,
                      @ApiParam(value = "每页记录数",required = true) @PathVariable("limit") Long limit,
                      @ApiParam(value = "讲师列表查询对象,(条件查询对象)") TeacherVo teacherVo){
        Page<Teacher> teacherPage = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.selectPage(teacherPage,teacherVo);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam("讲师数据") @RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败，数据异常！");
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R update(@ApiParam("讲师数据") @RequestBody Teacher teacher){
        boolean updateById = teacherService.updateById(teacher);
        if(updateById){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败，数据异常！");
    }

    @ApiOperation("根据ID查询讲师数据")
    @GetMapping("get/{id}")
    public R getByIdTeacher(@ApiParam("讲师ID") @PathVariable("id") Long id){
        Teacher byId = teacherService.getById(id);
        if(byId!=null){
            return R.ok().data("data",byId);
        }
        return R.error().message("该讲师数据已被删除");
    }

    @ApiOperation("批量删除")
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@ApiParam(value = "讲师ID列表",required = true) @RequestBody List<String> idList){
        boolean b = teacherService.removeByIds(idList);
        if(b){
            return R.ok().message("删除成功！");
        }
        return R.error().message("数据异常");
    }

    @ApiOperation("根据关键字查询讲师列表数据")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@ApiParam(value = "关键字",required = true) @PathVariable("key") String key){
        List<Map<String,Object>> nameList=teacherService.selectNameListByKey(key);
        return R.ok().data("nameList",nameList);
    }
}

